/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Autenticacion.java
 *
 * Created on 21-ene-2010, 20:25:42
 */

package vista;

import modelo.persistencia.SistAutenticacionDAO;
import modelo.persistencia.ISistAutenticacionDAO;
import modelo.ISistAutenticacion;
import modelo.SistAutenticacion;

import java.util.*;
/**
 *
 * @author Juanma
 */
public class JSistAutenticacionView extends javax.swing.JFrame {

    /** Creates new form Autenticacion */
    public JSistAutenticacionView() {
        initComponents();
    }
    public java.awt.TextField gettext1 ()
    {
        return (textField1);
    }

    public java.awt.TextField gettext2 ()
    {
        return (textField2);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label1 = new java.awt.Label();
        textField1 = new java.awt.TextField();
        label2 = new java.awt.Label();
        label3 = new java.awt.Label();
        label4 = new java.awt.Label();
        textField2 = new java.awt.TextField();
        button1 = new java.awt.Button();
        button2 = new java.awt.Button();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ventana de acceso al sistema");

        label1.setText("Bienvenido al Sistema S3GLS");

        label2.setText("Usuario");

        label3.setText("Password");

        label4.setText("Introduzca Nombre de Usuario y Contraseña :");

        textField2.setEchoChar('*');

        button1.setLabel("Entrar");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        button2.setActionCommand("Borrar");
        button2.setLabel("Borrar");
        button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(textField1, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                                    .addComponent(textField2, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE))))))
                .addGap(82, 82, 82))
            .addGroup(layout.createSequentialGroup()
                .addGap(157, 157, 157)
                .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(111, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(94, 94, 94))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed

        // SISTEMA DE AUTENTICACION
        String usuario=this.textField1.getText();
        String password=this.textField2.getText();
        this.setBlanco();
        SistAutenticacionDAO dao=new SistAutenticacionDAO();
        List<ISistAutenticacion> personas=new ArrayList();
        personas=dao.list();
        Iterator it=personas.listIterator();
        ISistAutenticacion a=null;
        boolean enc=false;
        while(it.hasNext() && enc==false){
            a=(ISistAutenticacion)it.next();
            if(a.getUsuario().equals(usuario) && a.getPassword().equals(password)){
                enc=true;
            }
        }

        usuario=null;
        password=null;

        if(enc){
            System.out.println("usuario: " + a.getUsuario() + " password: " + a.getPassword() + " rol: " + a.getRol());
            if(a.getRol().equalsIgnoreCase("empleado")){
                java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaEmpleados().setVisible(true);
            }
            });
            }else if(a.getRol().equalsIgnoreCase("encargado")){
               java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaEncargados().setVisible(true);
            }
            });
            }else if(a.getRol().equalsIgnoreCase("rrhh")){
               java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaRecursosHumanos().setVisible(true);
            }
            });
            }else if(a.getRol().equalsIgnoreCase("admin")){
               java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaDirección().setVisible(true);
            }
            });
            }else if(a.getRol().equalsIgnoreCase("candidato")){
               java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AccesoDenegado().setVisible(true);
            }
            });
            }
        }else{
            java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ApagarSistema().setVisible(true);
            }
            });
        }
    }//GEN-LAST:event_button1ActionPerformed

    private void button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button2ActionPerformed
        this.setBlanco();
    }//GEN-LAST:event_button2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JSistAutenticacionView().setVisible(true);
            }
        });
    }

    public void setBlanco(){
        this.textField1.setText("");
        this.textField2.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button button1;
    private java.awt.Button button2;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.Label label4;
    private java.awt.TextField textField1;
    private java.awt.TextField textField2;
    // End of variables declaration//GEN-END:variables

}

